// pages/基本信息/基本信息.js
Page({
  data: {
    basic_info: {
      name: '',
      academy: '',
      licence_number: '',
      contact_infomation: ''
    }
  },

  onLoad(options) {
    const app = getApp()
    this.setData({
      'basic_info.name': app.globalData.basic_info.name,
      'basic_info.academy': app.globalData.basic_info.academy,
      'basic_info.licence_number': app.globalData.basic_info.licence_number,
      'basic_info.contact_infomation': app.globalData.basic_info.contact_infomation
    })
  },

  name(event) {
    this.setData({
      'basic_info.name': event.detail.value
    })
  },

  academy(event) {
    this.setData({
      'basic_info.academy': event.detail.value
    })
  },

  licence_number(event) {
    this.setData({
      'basic_info.licence_number': event.detail.value.toString()
    })
  },

  contact_infomation(event) {
    this.setData({
      'basic_info.contact_infomation': event.detail.value
    })
  },

  confirm(event) {
    const that = this
    const app = getApp()
    wx.showModal({
      title: '基本信息',
      content: '确认基本信息填写吗？',
      complete: (res) => {
        if (res.confirm) {
          app.globalData.basic_info = that.data.basic_info
          that.setData({
            'basic_info.user_id': app.globalData.user_id,
            'basic_info.avatar': app.globalData.login.avatar
          })
          wx.request({
            url: app.globalData.root_url + 'basic_infomation/',
            method: 'POST',
            data: JSON.stringify(this.data.basic_info),
            success(res) {
              if (res.data === 1) {
                wx.showModal({
                  title: '基本信息',
                  content: '更新成功',
                  showCancel: false,
                  complete: (res) => {
                    if (res.confirm) {
                      wx.navigateBack()
                    }
                  }
                })
              }
            }
          })
        }
      }
    })
  }
})